package com.yuke.cloud.common.security.core.social.weixin.config;

import com.yuke.cloud.common.security.core.properties.SecurityProperties;
import com.yuke.cloud.common.security.core.properties.WeixinProperties;
import com.yuke.cloud.common.security.core.social.view.PcConnectView;
import com.yuke.cloud.common.security.core.social.weixin.connect.WeixinConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.web.servlet.View;

/**
 * 微信登录配置
 *
 * @author
 */
//@Configuration
//@ConditionalOnProperty(prefix = "yuke.security.social.weixin", name = "app-id")
//public class WeixinAutoConfiguration extends SocialConfigurerAdapter {
//
//	@Autowired
//	private SecurityProperties securityProperties;
//
//	/**
//	 * Create connection factory connection factory.
//	 *
//	 * @return the connection factory
//	 */
//	//@Override
//	protected ConnectionFactory<?> createConnectionFactory() {
//		WeixinProperties weixinConfig = securityProperties.getSocial().getWeixin();
//		return new WeixinConnectionFactory(weixinConfig.getProviderId(), weixinConfig.getAppId(),
//				weixinConfig.getAppSecret());
//	}
//
//	/**
//	 * Weixin connected view view.
//	 *
//	 * @return the view
//	 */
//	@Bean({"connect/weixinConnect", "connect/weixinConnected"})
//	@ConditionalOnMissingBean(name = "weixinConnectedView")
//	public View weixinConnectedView() {
//		return new PcConnectView();
//	}
//
//}

/**
 * autoconfigure2.04中已经不存在social的自动配置类了
 * org.springframework.boot.autoconfigure.social.SocialAutoConfigurerAdapter
 * @author : zhuqiang
 * @version : V1.0
 * @date : 2018/8/6 9:20
 */
@Configuration
// 当配置了app-id的时候才启用
@ConditionalOnProperty(prefix = "yuke.security.social.weixin", name = "app-id")
public class WeixinAutoConfiguration extends SocialConfigurerAdapter {

	private final SecurityProperties securityProperties;

	@Autowired
	public WeixinAutoConfiguration(SecurityProperties securityProperties) {
		this.securityProperties = securityProperties;
	}

	@Override
	public void addConnectionFactories(ConnectionFactoryConfigurer configurer,
									   Environment environment) {
		configurer.addConnectionFactory(createConnectionFactory());
	}

	/**
	 * Create connection factory connection factory.
	 *
	 * @return the connection factory
	 */
	//@Override
	protected ConnectionFactory<?> createConnectionFactory() {
		WeixinProperties weixinConfig = securityProperties.getSocial().getWeixin();
		return new WeixinConnectionFactory(weixinConfig.getProviderId(), weixinConfig.getAppId(),
				weixinConfig.getAppSecret());
	}

	// 后补：做到处理注册逻辑的时候发现的一个bug：登录完成后，数据库没有数据，但是再次登录却不用注册了
	// 就怀疑是否是在内存中存储了。结果果然发现这里父类的内存ConnectionRepository覆盖了SocialConfig中配置的jdbcConnectionRepository
	// 这里需要返回null，否则会返回内存的 ConnectionRepository
	@Override
	public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
		return null;
	}
}
